package cn.js.sangu.unitStore.service;

import cn.js.sangu.unitStore.common.exception.ServiceException;
import cn.js.sangu.unitStore.entity.Doc;
import cn.js.sangu.unitStore.mapper.DocMapper;
import cn.js.sangu.unitStore.utils.LinsUtil;
import cn.js.sangu.unitStore.utils.TokenUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 说明：文档服务
 * 作者：linshuo
 */
@Slf4j
@Service
public class DocService extends ServiceImpl<DocMapper, Doc> {

//    @Resource
//    DocMapper docMapper;

    public Doc saveInfo(Doc dto){
        dto.setAuthor(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId());
        this.keywordReplace(dto);
        if (!super.save(dto)){
            throw new ServiceException("保存失败！请联系管理员");
        }
        return dto;
    }

    public Doc updateInfo(Doc dto){
        dto.setModifier(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId());
        this.keywordReplace(dto);
        if (!super.updateById(dto)){
            throw new ServiceException("更新失败！请联系管理员");
        }
        return dto;
    }
   /*
    updata function(res ：string){
        return res;
        var a:string = ‘1’
   }
   */ 

    private void keywordReplace(Doc dto){
        dto.setTitle(LinsUtil.keywordReplace(dto.getTitle(), null, null));
        dto.setContent(LinsUtil.keywordReplace(dto.getContent(), null, null));
    }

}
